<?php
namespace app\api\model;
use think\Config;
use think\Db;
class ChatModel extends \app\common\model\CommonModel
{
    
    /**
     * 写入标题
     * @param string $userid
     * @param string $chatid
     * @param string $title
     */
    public function insert_title($userid,$chatid,$title)
    {
        try {
            return Db::table('chat')->insert([
                'userid'    =>  $userid,
                'chatid'    =>  $chatid,
                'title'     =>  $title,
                'status'    =>  1,
            ]);        
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 会话列表
     * @param string $userid
     */
    public function list($userid)
    {
        try {
            $res = Db::table('chat')->where(['userid' => $userid])->where(['status' => 1])->order('create_time desc')->select();
            if($res){
                foreach ($res as $key => $value) {
                    $res[$key]['chat_info_json'] = "https://xc.souxiu.cn/jxgpt/" . $value['chatid'] . '.json';
                }
            }
            return $res;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 编辑会话标题
     * @param string $chatid
     * @param string $title
     */
    public function edit($userid,$data)
    {
        try {
            return Db::table('chat')->where(['userid' => $userid,'chatid' => $data['chatid']])->update(['title' => trim($data['title'])]);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 删除会话
     * @param string $userid
     * @param string $chatid
     */
    public function del($userid,$chatid)
    {
        try {
            return Db::table('chat')->where(['userid' => $userid,'chatid' => $chatid])->update(['status' => 2]);    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    public function info($chatid, $params)
    {
        try {
            // $res = Db::table('chat')->where(['chatid' => $chatid])->find();
            // if(!$res){
            //     return [];
            // }
            // return Db::table('message')->where(['chatid' => $chatid])->order('create_time asc')->select();
            $page       = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize   = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start      = ($page-1) * $pagesize;
            $res = Db::table('chat')
                    ->alias('a')
                    ->field('a.*,b.username')
                    ->join('user b','a.userid = b.userid')
                    ->order('a.id desc')
                    ->limit($start, $pagesize)
                    ->where("a.userid != 'SS2019070002'")
                    ->select();
            if ($res) {
                $res = array_map(function($v){
                    
                    $v['chat_detail'] = Db::table('message')->where(['chatid' => $v['chatid']])->order('create_time asc')->select();
                    return $v;
                },$res);
            }
            return $res;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }    
    }
}